<?php
 require "../document/class_corpus.php";
 require "../document/class_document.php";
 require "../document/class_document_xml.php";

 require "../files/tool_files_dirs.php";
 require "../misc/class_tag.php";

 require "../tree/class_tree.php";
 require "../tree/class_tree_xhtml.php";
 require "../tree/class_structure.php";
 require "../tree/tool_tree.php";

 require "../array/tool_array.php";

 require "../string/tool_string.php";
 require "../string/class_segment.php";

 require "../pdo/class_sql_document.php";

$exp=new sql_experience("align_ecr_word");
$array_obj_sql=$exp->reader_experience();
$array_data=array();

$data_lg=$array_obj_sql['sql_document']->reader_all_lg();

$corpus_multidoc = array();

//$data_lg = array( array("lg"=>"fr") );
$id_chunk = 1;

$array_factory = array();
foreach($data_lg as $lg){
  $array_factory[$lg['lg']] = new factory_mot();
  $r = $array_obj_sql['sql_rstr']->reader_all2relative_id_doc($lg["lg"]);
  $k = key($r);
  foreach($r[$k] as $tid=>$id){
    $corpus_multidoc[$id][$k] = $tid;
  }
}

$id_multidoc = 1;
$multidoc = $corpus_multidoc[$id_multidoc];

$doc2bloc = array();
$id_bloc2text = array();

foreach($corpus_multidoc as $id_multidoc=>$multidoc){
  echo "id_multidoc :: $id_multidoc\n";
  foreach($multidoc as $lg=>$id_doc){
  $r = $array_obj_sql['sql_bloc']->reader_id_document2array($id_doc);
  foreach($r as $info_bloc){
    $id_bloc = $info_bloc['id_bloc'];
    $id_bloc2text[$id_bloc] = $info_bloc['contenu_text'];
    $array_word = $array_obj_sql['sql_word']->reader_id_bloc2info_word($id_bloc);

    foreach($array_word as $word){
      $mot = new mot($id_bloc2text[$id_bloc],
                     $word['offset'],$word['len'],
                     array("id_bloc"=>$id_bloc,"id_doc"=>$lg,"id_word"=>$word['id_word']));
      $array_factory[$lg]->__add_word($mot,$lg,$id_bloc);
    }
  }
  $doc2bloc[$lg] = $r;
  }
}

foreach($array_factory as $lg=>$factory){
  $array_data = array();
  print "$lg::e\n";
  $factory->init_etiquetage();
  print "$lg::c\n";
  $factory->init_chunk();
  print "$lg::get\n";
  $rep = $factory->__get_repartition_chunk();
  print "$lg::svg\n";
  foreach($rep as $id_doc=>$array_doc){
    foreach($array_doc as $id_bloc=>$array_bloc){
      foreach($array_bloc as $chunk){
        foreach($chunk as $id_word){
          $word = $factory->__get_segment($id_word);
          $id_word = $word->__get_id_word();
          $type = $word->__get_type();
          $array_data[]=array(
            'id_chunk'=>$id_chunk,
            'id_word'=>$id_word,
            'type'=>$type,
          );
        }
        ++$id_chunk;
      }
    }
  }
  $array_obj_sql['sql_chunk']->writer_array_values($array_data);
  unset($array_data);
}

print $id_chunk;
//  tool_factory_mot::factory2print($factory_mot);
  die();
  print_r($r);
  die();

print_r($doc2bloc["fr"]);

die();


$data=$array_obj_sql['sql_bloc']->reader_all2exec_plus_lg();
$factory_mot=new factory_mot();
foreach($data as $ligne){
  print_r($ligne);
  die();
  $factory_mot->get_all_mots($ligne['contenu_text']);
}


die();

$a1 = array('le','petit','chat');

$factory_mot=new factory_mot();

foreach($a1 as $w){
  $mot = new mot();
  $factory_mot->__add_word();
}


$factory_mot->get_all_mots($a[0]);
$factory_mot->get_all_mots($a[1]);
$factory_mot->get_all_mots($a[2]);

$factory_mot->init_etiquetage();
$factory_mot->init_chunk();
$time_end = microtime(true);
$time=$time_end-$time_start;
echo "time=$time\n";

tool_factory_mot::factory2print($factory_mot);

die();



 $corpus=tool_corpus::get_corpus_regexp("../../../Corpora/europa_complete_repair/","4.fr.xml");
// $corpus=tool_corpus::get_corpus_regexp("../xml/test/",".fr.xml");
 $array_doc=$corpus->__get_array_doc();
 $document=$array_doc[0];
// print_r($document);
// die();
 $structure=$document->__get_structure();
 $signature=$structure->__get_signature();
 tool_tree::cleanstructure($signature);
 $signature->__recalc_idn();

 //echo tool_tree::structure2texte($signature);
 $test=new RecursiveIteratorIterator($signature,RecursiveIteratorIterator::SELF_FIRST);

 $factory_mot=new factory_mot();

 $time_start = microtime(true);
 foreach($test as $bloc_xhtml){
    $texte=tool_tree::tree_xhtml2texte($bloc_xhtml->__get_value());
    $id=$factory_mot->get_all_mots($texte);
//    $factory_mot->get_array_words($texte);
//    echo "\r$id";
   //print_r(tool_tree::tree_xhtml2texte($bloc_xhtml));
   //echo $bloc_xhtml;
 }

$factory_mot->init_etiquetage();
$factory_mot->init_chunk();
$time_end = microtime(true);
$time=$time_end-$time_start;
echo "time=$time\n";

tool_factory_mot::factory2print($factory_mot);

//tool_factory_mot::factory2dictionnairetexte($factory_mot);
// tool_factory_mot::factory2texte($factory_mot); 
// echo tool_tree::structure2texte($signature);
// print_r(count($array_doc));
die();

foreach($array_doc as $doc){
 $structure=$doc->__get_structure();
 $signature=$structure->__get_signature();
 //echo get_class($structure);
 //print_r($signature);
 echo tool_tree::structure2texte($signature); 
}



die();
 $array_ids_europa=tool_corpus::europa_get_all_ids("../xml/europa/");
 $array_corpus_groupe=array();

 $time_start = microtime(true);
 foreach($array_ids_europa as $id_europa){
  $corpus=tool_corpus::get_corpus_regexp("../xml/europa/",$id_europa);
  $array_corpus_groupe[$id_europa]=array();
  //echo "$id_europa\n";
  foreach($corpus->__get_array_doc() as $document){
    $structure=$document->__get_structure();
    $signature=$structure->__get_signature();
    tool_tree::cleanstructure($signature);
    $signature->__recalc_idn();
    echo tool_tree::structure2texte($signature);
  }
  die();
 }
 die();


?>
